// pages/outvisit/task/inputInfo.js
var api = require("../../../utils/api.js");
var common = require("../../../utils/common.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pickerValue: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],//下拉框的值
    pickerValuePuFa: [0, 0],
    id: 0,//任务ID
    collectionCaseId: 0,//催收案件ID
    trustor: "",//委托方
    pictureFiles: [],//照片列表
    videoFiles: [],//视频列表
    recordFiles: [],//录音列表
    whetherList: common.whetherList,//是否列表
    addressTypeList: common.addressTypeList,//地址类型列表
    haveMailList: common.haveMailList,//信箱情况列表
    whoAnsweredDoorList: common.whoAnsweredDoorList,//见到对象列表
    targetWhosList: common.targetWhosList,//排查对象列表
    noHaveReasonList: common.noHaveReasonList,//地址不存在原因列表
    addressInfoList: common.addressInfoList,//地址情况列表
    homeownersList: common.homeownersList,//房屋所有人列表
    customerSituationList: common.customerSituationList,//客户工作情况列表
    overdueCauseList: common.overdueCauseList,//逾期原因列表
    resultCodeList: common.resultCodeList,//结果代码列表
    ccodeList: common.ccodeList,//外访结果列表（浦发）
    rcodeList: [],//外访场景列表（浦发）
    outvistRecord: {},//外访记录
    outvistDates: "",//外访日期
    isNoUpdate: true,//是否不允许更新
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    var collectionCaseId = options.collectionCaseId;
    var trustor = options.trustor;
    // 设置是否不允许更新
    that.setIsNoUpdate();
    // 设置任务ID
    that.setData({
      id: id,
      collectionCaseId: collectionCaseId,
      trustor: trustor
    }, () => {
      // 获取外访记录
      that.getOutvistRecord();
    });
  },

  // 设置是否不允许更新
  setIsNoUpdate: function () {
    var that = this;
    var roleList = wx.getStorageSync("roleList");
    if (roleList && roleList != "undefined" && roleList.length > 0) {
      if (roleList.indexOf("系统管理员") != -1) {
        that.setData({
          isNoUpdate: false
        });
      }
    }
  },

  // 获取外访记录
  getOutvistRecord: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
    });//显示加载中
    api.getOutvistRecord(that.data.id).then(res => {
      if (res.code == 0) {
        wx.hideLoading();//隐藏加载中
        var data = res.data;
        if (data) {
          var files = data.files;
          if (files && files.length > 0) {
            // 设置文件列表
            that.setFiles(files);
          }
          // 设置表单下拉框默认值（浦发）
          if (that.data.trustor == "浦发银行") {
            that.setPickerValuePuFa(data);
          }
          // 设置表单下拉框默认值
          that.setPickerValue(data);
          // 设置排查对象默认值
          that.setTargetWhosValue(data);
          that.setData({
            outvistRecord: data,
            outvistDates: data.dates
          });
        }
      }
    });
  },

  // 设置文件列表
  setFiles: function (files) {
    var that = this;
    var pictureFiles = [];//照片列表
    var videoFiles = [];//视频列表
    var recordFiles = [];//录音列表
    for (var i = 0; i < files.length; i++) {
      if (files[i].fileType==1){
        var file = files[i];
        if (!file.photoType) {
          file.photoType = 1;
        }
        pictureFiles.push(file);
      } else if (files[i].fileType == 2) {
        videoFiles.push(files[i]);
      } else if (files[i].fileType == 3) {
        recordFiles.push(files[i]);
      }
    }
    that.setData({
      pictureFiles: pictureFiles,
      videoFiles: videoFiles,
      recordFiles: recordFiles
    });
  },

  // 设置表单下拉框默认值（浦发）
  setPickerValuePuFa: function (data) {
    var that = this;
    var pickerValuePuFa = that.data.pickerValuePuFa;
    if (data.ccode) {
      var ccodeList = that.data.ccodeList;
      for (var i = 0; i < ccodeList.length; i++) {
        if (data.ccode == ccodeList[i].id) {
          pickerValuePuFa[0] = i;
        }
      }
      // 获取外访场景列表（浦发）
      wx.showLoading({
        title: '加载中',
      });//显示加载中
      api.getSceneData(that.data.ccodeList[pickerValuePuFa[0]].id).then(res => {
        if (res.code == 0) {
          wx.hideLoading();//隐藏加载中
          var sceneData = res.data;
          var rcodeList = [];
          if (sceneData) {
            rcodeList = sceneData;
          }
          if (data.rcode) {
            for (var i = 0; i < rcodeList.length; i++) {
              if (data.rcode == rcodeList[i].id) {
                pickerValuePuFa[1] = i;
              }
            }
          }
          that.setData({
            pickerValuePuFa: pickerValuePuFa,
            rcodeList: rcodeList
          });
        }
      });
    }
  },

  // 设置表单下拉框默认值
  setPickerValue: function (data) {
    var that = this;
    var pickerValue = that.data.pickerValue;
    if (data.addressType) {
      pickerValue[0] = data.addressType;
    }
    if (data.effective) {
      pickerValue[1] = data.effective;
    }
    if (data.haveMail) {
      pickerValue[2] = data.haveMail;
    }
    if (data.havePerson) {
      pickerValue[3] = data.havePerson;
    }
    if (data.whoAnsweredDoor) {
      pickerValue[4] = data.whoAnsweredDoor;
    }
    if (data.noHaveReason) {
      pickerValue[5] = data.noHaveReason;
    }
    if (data.addressInfo) {
      pickerValue[6] = data.addressInfo;
    }
    if (data.homeowners) {
      pickerValue[7] = data.homeowners;
    }
    if (data.customerSituation) {
      pickerValue[8] = data.customerSituation;
    }
    if (data.overdueCause) {
      pickerValue[9] = data.overdueCause;
    }
    if (data.resultCode) {
      pickerValue[10] = data.resultCode;
    }
    that.setData({
      pickerValue: pickerValue
    });
  },

  // 设置排查对象默认值
  setTargetWhosValue: function (data) {
    var that = this;
    var targetWhos = data.targetWhos;
    var targetWhosValue = [];
    var targetWhosList = that.data.targetWhosList;
    if (targetWhos) {
      targetWhosValue = targetWhos.split(",");
    }
    for (var i = 0; i < targetWhosValue.length; i++) {
      for (var j = 0; j < targetWhosList.length; j++) {
        if (targetWhosValue[i] == targetWhosList[j].value) {
          targetWhosList[j].checked = "true";
        }
      }
    }
    that.setData({
      targetWhosList: targetWhosList
    });
  },

  // 提交表单
  formSubmit: function (e) {
    var that = this;
    if (that.data.isNoUpdate){
      wx.showModal({
        title: "提示",
        content: "确定要提交吗？",
        success: function (sm) {
          if (sm.confirm) {
            // 提交表单数据
            that.formSubmitData(e);
          }
        }
      })
    } else {
      // 提交表单数据
      that.formSubmitData(e);
    }
  },

  // 提交表单数据
  formSubmitData: function (e) {
    var that = this;
    var data = e.detail.value;
    var submitData = {};
    for (var d in data) {
      // if (data[d].trim()) {
      submitData[d] = data[d];
      // }
    }

    // 浦发专用参数
    if (that.data.trustor == "浦发银行") {
      if (submitData.ccode == 0) {
        wx.showToast({
          title: "请选择外访结果",
          icon: "none"
        });
        return;
      }
      if (submitData.addressType == 0) {
        wx.showToast({
          title: "请选择地址类型",
          icon: "none"
        });
        return;
      }

      var pcode = "";
      if (submitData.addressType == 1 || submitData.addressType == 2 || submitData.addressType == 4){
        pcode = "H";
      } else if (submitData.addressType == 3) {
        pcode = "B";
      } else {
        pcode = "O";
      }
      submitData.pcode = pcode;
      submitData.ccode = that.data.ccodeList[submitData.ccode].id;
      submitData.rcode = that.data.rcodeList[submitData.rcode].id;
    }

    submitData.outboundAnalysis = submitData.outboundAnalysis.trim();
    submitData.targetWhos = submitData.targetWhos.join(',');
    var files = [].concat(that.data.pictureFiles, that.data.videoFiles, that.data.recordFiles);
    submitData.files = files;
    wx.showLoading({
      title: '录入中'
    });//显示加载中
    api.setOutvistRecord(submitData).then(res => {
      if (res.code == 0) {
        wx.hideLoading();
        wx.navigateBack({
          delta: 1,
          success: function () {
            wx.showToast({
              title: "录入成功",
              icon: "none"
            });
          }
        });
      }
    });
  },

  // 获取外访场景列表（浦发）
  getSceneData: function (ccode) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    });//显示加载中
    api.getSceneData(ccode).then(res => {
      if (res.code == 0) {
        wx.hideLoading();//隐藏加载中
        var data = res.data;
        var rcodeList = [];
        if (data) {
          rcodeList = data;
        }

        let pickerValuePuFa = that.data.pickerValuePuFa;
        pickerValuePuFa[1] = 0;
        that.setData({
          pickerValuePuFa: pickerValuePuFa,
          rcodeList: rcodeList
        });
      }
    });
  },

  // 上传点击事件
  uploadClick: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: 'uploadList?outvisitTaskId=' + that.data.id + '&type=' + type + '&collectionCaseId=' + that.data.collectionCaseId
    });
  },
  
  //时间选择
  bindDateChange(e) {
    let that = this;
    that.setData({
      outvistDates: e.detail.value
    })
  },

  //下拉框选择事件
  bindPickerChange: function (e) {
    let pickerValue = this.data.pickerValue;
    pickerValue[parseInt(e.currentTarget.dataset.id)] = e.detail.value;
    this.setData({
      pickerValue: pickerValue
    });
  },

  //下拉框选择事件（浦发）
  bindPickerChangePuFa: function (e) {
    let pickerValuePuFa = this.data.pickerValuePuFa;
    pickerValuePuFa[parseInt(e.currentTarget.dataset.id)] = e.detail.value;
    this.setData({
      pickerValuePuFa: pickerValuePuFa
    });

    if (e.currentTarget.dataset.id == 0){
      // 获取外访场景列表（浦发）
      this.getSceneData(this.data.ccodeList[e.detail.value].id);
    }
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})